package guowenlong.golf.ui.friendlymatch;

import android.Manifest.permission;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.OnClick;
import cc.solart.turbo.OnItemClickListener;
import cc.solart.wave.WaveSideBarView;
import cc.solart.wave.WaveSideBarView.OnTouchLetterChangeListener;
import guowenlong.golf.R;
import guowenlong.golf.ui.friend.myfriends.ContactsInfo;
import guowenlong.golf.ui.friend.myfriends.LetterComparator;
import guowenlong.golf.ui.friend.myfriends.PinnedHeaderDecoration;
import guowenlong.golf.ui.friend.myfriends.PinnedHeaderDecoration.PinnedHeaderCreator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import me.wenlong.mixutils.mvp.BaseActivity;

/**
 * des   : 描述
 * author: Administrator
 * email : guowenlong20000@gmail.com
 * time  : 2017年05月03日 上午 1:10.
 */

public class MainscoreActivity extends BaseActivity {

  @BindView(R.id.tv_back)
  TextView tvBack;
  @BindView(R.id.recycler_view)
  RecyclerView mRecyclerView;
  @BindView(R.id.side_view)
  WaveSideBarView mSideBarView;
  @BindView(R.id.btn_selected_save)
  Button btnSelectedSave;
  private SelectedFriendlyAdapter adapter;
  private ArrayList<ContactsInfo> templists = new ArrayList<>();
  private String[] strs = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
      "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
  public static List<ContactsInfo> list = new ArrayList<>();

  @Override
  protected int getContentView(Bundle savedInstanceState) {
    return R.layout.activity_main_score;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    mRecyclerView.setLayoutManager(new LinearLayoutManager(MainscoreActivity.this));

    final PinnedHeaderDecoration decoration = new PinnedHeaderDecoration();
    decoration.registerTypePinnedHeader(1, new PinnedHeaderCreator() {
      @Override
      public boolean create(RecyclerView parent, int adapterPosition) {
        return true;
      }
    });
    mRecyclerView.addItemDecoration(decoration);
    if (ContextCompat
        .checkSelfPermission(MainscoreActivity.this, permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {
      ActivityCompat.requestPermissions(MainscoreActivity.this,
          new String[]{permission.READ_CONTACTS},
          1);
    } else {
      getContans();
    }
    adapter = new SelectedFriendlyAdapter(MainscoreActivity.this, templists);
    mRecyclerView.setAdapter(adapter);
    mSideBarView.setOnTouchLetterChangeListener(new OnTouchLetterChangeListener() {
      @Override
      public void onLetterChange(String letter) {
        int pos = adapter.getLetterPosition(letter);
        if (pos != -1) {
          mRecyclerView.scrollToPosition(pos);
          LinearLayoutManager mLayoutManager =
              (LinearLayoutManager) mRecyclerView.getLayoutManager();
          mLayoutManager.scrollToPositionWithOffset(pos, 0);
        }
      }
    });
    adapter.addOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(ViewHolder vh, int position) {
        int count = 0;
        for (int i = 0; i < templists.size(); i++) {
          if (templists.get(i).isSelected()) {
            count++;
          }
        }
        boolean selected = templists.get(position).isSelected();
        if (!selected) {
          if (count >= 1) {
            Toast.makeText(MainscoreActivity.this, "不能超过一个人", Toast.LENGTH_SHORT).show();
            adapter.setChecked(position, false);
          } else {
            templists.get(position).setSelected(!templists.get(position).isSelected());
            adapter.setChecked(position, !selected);
          }
        } else {
          templists.get(position).setSelected(!templists.get(position).isSelected());
          adapter.setChecked(position, !selected);

        }
      }
    });
  }

  @Override
  protected void initData() {

  }

  public void getContans() {
    list.clear();
    templists.clear();
    ContactsInfo contactsInfo1 = new ContactsInfo("陈七", "11111111111", "C", 1, 0);
    ContactsInfo contactsInfo2 = new ContactsInfo("李四", "11111111111", "L", 2, 0);
    ContactsInfo contactsInfo3 = new ContactsInfo("王五", "11111111111", "W", 3, 0);
    ContactsInfo contactsInfo4 = new ContactsInfo("赵六", "11111111111", "Z", 4, 0);
    ContactsInfo contactsInfo5 = new ContactsInfo("张三", "11111111111", "Z", 5, 0);
    list.add(contactsInfo1);
    list.add(contactsInfo2);
    list.add(contactsInfo3);
    list.add(contactsInfo4);
    list.add(contactsInfo5);
    for (int i = 0; i < strs.length; i++) {
      for (int j = 0; j < list.size(); j++) {
        if (list.get(j).getSortKey() != null && list.get(j).getSortKey().equals(strs[i])) {
          templists.add(new ContactsInfo(strs[i], 1));
          break;
        }
      }
    }
    templists.addAll(list);
    Collections.sort(templists, new LetterComparator());
  }

  @OnClick({R.id.tv_back, R.id.btn_selected_save})
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.tv_back:
        finish();
        break;
      case R.id.btn_selected_save:
        ArrayList<ContactsInfo> contactsInfos = new ArrayList<>();
        for (int i = 0; i < templists.size(); i++) {
          boolean selected = templists.get(i).isSelected();
          if (selected) {
            contactsInfos.add(templists.get(i));
          }
        }
        if (contactsInfos.size() == 0) {
          Toast.makeText(this, "请选择一个人", Toast.LENGTH_SHORT).show();
        }
        Intent intent = new Intent(this, FriendlyMatchActivity.class);
        intent.putExtra("list", contactsInfos);
        setResult(RESULT_OK, intent);
        finish();
        break;
    }
  }
}